package com.sanjiahantang.youjiaclient.module.common.models.impl;

import android.content.Context;

import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.http.BaseDelegate;
import com.sanjiahantang.youjiaclient.http.OkHttpClientManager;
import com.sanjiahantang.youjiaclient.module.common.beans.HomeBean;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.common.models.IHomeModel;
import com.sanjiahantang.youjiaclient.module.common.views.HomeView;
import com.sanjiahantang.youjiaclient.module.match.beans.MatchListBean;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.DEFAULT_LAT;
import static com.sanjiahantang.youjiaclient.config.StaticValue.DEFAULT_LON;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_LAT;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_LON;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TOKEN;

/**
 * Created by zy on 2017/9/1.
 */

public class HomeModel implements IHomeModel {


    @Override
    public void getHomePageData(final Context context,final CallBack<HomeBean> callBack) {
        /*if(ConfigValue.isDebug){


            new Handler().postDelayed(new Runnable() {
                public void run() {
                    HomeBean model = null;
                    String json = logJson();
                    Log.e("aaa",json);
                    model = new Gson().fromJson(json,HomeBean.class);
                    callBack.onSuccess(model,context);
                }
            }, 1000);
            return;
        }*/



        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY == null ? "" : ConfigValue.DATA_KEY);
        param.put(KEY_LAT, String.valueOf(ConfigValue.lat != 0 ? ConfigValue.lat : DEFAULT_LAT));
        param.put(KEY_LON, String.valueOf(ConfigValue.lon != 0 ? ConfigValue.lon : DEFAULT_LON));
        param.put(HomeView.KEY_ADDRESS,ConfigValue.city != null ? ConfigValue.city : HomeView.DEFAULT_CITY);
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/index/customer/index",param,new BaseDelegate.ResultCallback<HomeBean>() {

            @Override
            public void onError(Request request, Object tag, Exception e) {
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(HomeBean response, Object tag) {
                callBack.onSuccess(response,context, null);
            }
        });
    }

    @Override
    public void getMoreHomePageData(final Context context, Map<String,String> param, final CallBack<MatchListBean> callBack) {

    }

    @Override
    public void findToken(final Context context, final CallBack<StringBean> callBack) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/customer/findToken",param,new BaseDelegate.ResultCallback<StringBean>() {

            @Override
            public void onError(Request request, Object tag, Exception e) {
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(StringBean response, Object tag) {
                callBack.onSuccess(response,context, null);
            }
        });
    }
}
